<?php
	/**
 	 * Application configuration controller class
 	 */
	class AppConfigController extends Controller
	{
		/**
	 	 * page title
	 	 */
		public $page_title = LANG_CONFIGURATION_FILE;
		
		/**
	 	 * page layout
	 	 */
		public $layout = 'main';
		
		/**
	 	 * necessary helpers
	 	 */
		public $helpers = array ("html", "htmlx", "toolbars");
		
		/**
	 	 * necessary javascript files
	 	 */
		public $javascript = array("TreeMenu");
		
		/**
	 	 * necessary components
	 	 */
		public $components = array ("TreeMenu");
		
		
		/**
	 	 * displays index page
	 	 */
		function index()
		{
			//fills the config file
			if (!empty($this->request->post))
			{
				//makes copy of the file
				copy('app'.SEPARATOR.'config.php','app'.SEPARATOR.'config_backup.php');
				
				//opens the file for writing
				$fp = fopen('app'.SEPARATOR.'config.php', 'w+');
				
				fwrite($fp, "<?php \n");
				
				fwrite($fp, "\t/**\n\t* Application configuration class\n\t*/\n\tclass Config\n\t{\n");
				
				//writes variables
				foreach($this->request->post[variables] as $key => $value)
				{
					$comment = "\t\t/**\n\t\t * ".trim(str_ireplace(LANG_DESCRIPTION.':', '', strip_tags($this->request->post[comments][$key])))."\n\t\t*/\n";
					$comment = str_ireplace(LANG_TYPE.': ', "\n\t\t *\n\t\t * @var", $comment);
					$output = $comment;
					
					if (($value == 'true')||($value == 'false'))
					{
						$output .= "\t\tpublic $key = $value; \n\n";
					}
					else
					{
						$output .= "\t\tpublic $key = \"$value\"; \n\n";
					}
					fwrite($fp, $output);
				}
				
				//writes arrays
				foreach($this->request->post[arrays] as $key => $value)
				{
					$comment = "\t\t/**\n\t\t * ".trim(str_ireplace(LANG_DESCRIPTION.':', '', strip_tags($this->request->post[comments][$key])))."\n\t\t*/\n";
					$comment = str_ireplace(LANG_TYPE.': ', "\n\t\t *\n\t\t * @var", $comment);
					$output = $comment;
					$output .= "\t\tpublic $$key = array \n\t\t(\n";
					
					foreach ($value as $k => $v)
					{
						$output .= "\t\t\t$k => \"$v\",\n";
					}
					
					$output .= "\t\t);\n\n";
					fwrite($fp, $output);
				}				
			
				fwrite($fp, "\n\t}");
				
				fwrite($fp, "\n?>");
				fclose($fp);	
			}		

			//reads the file
			$config_file = file('app'.SEPARATOR.'config.php');
			$trans_num = count($config_file);

			for ($i=1; $i<$trans_num; $i++)
			{
				$config_file[$i] = trim($config_file[$i]);
				
				//parse variables
				if ((ereg("^(public)", $config_file[$i]))&&(ereg(";$", $config_file[$i]))) 
				{
					//splits name and value
					$var = split('=', $config_file[$i]);
					
					//gets variable name
					ereg('\${1}[a-zA-Z_]+', $var[0], $var_name_matches);
					
					//removes quotes and ;
					$var[1] = ereg_replace("'|\"|;$", '', $var[1]);
					
					//adds to the variable array
					$this->data[variables][$var_name_matches[0]] = trim($var[1]);
					
					//removes slashes from the comment
					$comment = ereg_replace('^/|/$', '', $comment);
					
					//adds description label to the comment
					$comment = '<u>'.ucfirst(LANG_DESCRIPTION).'</u>: '.$comment;
					
					//adds type label to the comment
					$comment = str_replace(' @var', ' <u>'.ucfirst(LANG_TYPE).'</u>: ', $comment);
					
					//adds comments for the variable
					$this->data[comments][$var_name_matches[0]] = str_replace('*', '', $comment);
					
					//clears comment
					$comment = '';
				}
				
				//parse comment beginning
				else if (ereg("^(/\*)", $config_file[$i]))
				{
					$comment = $config_file[$i];
					$is_comment = true;
				}

				//parse comment
				else if ($is_comment)
				{
					//adds to the comment
					$comment .= $config_file[$i];

					//if end of comment is reached
					if (ereg("^(\*/)", $config_file[$i])) 
					{
						$is_comment = false;
					}
				}
				
				//parse array name
				else if (ereg("^(public)", $config_file[$i]))
				{
					ereg('\${1}[a-zA-Z_]+', $config_file[$i], $matches);
					$is_array = true;
					
					//removes slashes from the comment
					$comment = ereg_replace('^/|/$', '', $comment);
					
					//adds description label to the comment
					$comment = '<u>'.ucfirst(LANG_DESCRIPTION).'</u>: '.$comment;
					
					//adds type label to the comment
					$comment = str_replace(' @var', ' <u>'.ucfirst(LANG_TYPE).'</u>: ', $comment);
					
					//adds comments for the variable
					$this->data[comments][$matches[0]] = str_replace('*', '', $comment);
					
					//clears comment
					$comment = '';
				}
				
				//parse array elemenets
				else if ($is_array)
				{
					//if we deal with brackets etc
					if (!ereg("^[[:alnum:]]", $config_file[$i])) continue;
					
					//splits name and value
					$str = split('=>', $config_file[$i]);
					
					//gets the value
					ereg('\".*\"', $str[1], $value_match);
					$value_match[0] = str_replace('"', '', $value_match[0]);
					
					//adds to the arrays' array
					$this->data[arrays][$matches[0]][$str[0]] = $value_match[0];
					
					//if end of array is reached
					if (ereg(";$", $config_file[$i])) 
					{
						$is_array = false;
					}					
				}
			}
		}
	}
?>